package cn.itcast.nems.base.web.controller.o;


import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.base.api.ClazzApi;
import cn.itcast.nems.base.dto.ClazzDTO;
import cn.itcast.nems.base.dto.ClazzQueryDTO;
import cn.itcast.nems.base.web.vo.ClazzQueryVO;
import cn.itcast.nems.base.web.vo.ClazzVO;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import feign.Param;
import feign.RequestLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 学科
 * </p>
 *
 * @author zhouwei
 * @since 2023/9/5 4:59 PM
 **/
@RestController
@RequestMapping("/o/clazz")
public class ClazzController {
    @Autowired
    private ClazzApi clazzApi;

    /**
     * 班级分页查询
     * @param queryParam 请求参数
     * @return 分页结果
     */
    @PostMapping("/page/list")
    public ResponseEntity<?> findPageList(@RequestBody BasePageRequest<ClazzQueryDTO> queryParam){
        queryParam.convert();
        ClazzQueryDTO queryDTO = Objects.nonNull(queryParam.getCondition()) ? queryParam.getCondition(): new ClazzQueryDTO() ;
        // 过滤待定班
        queryDTO.setDelayFlag(false);
        queryDTO.setIncludeWait(false);
        queryDTO.setEnrolFlag(true);
        queryParam.setCondition(queryDTO);
        PageQueryModel<ClazzQueryDTO> queryModel =new PageQueryModel<>(queryParam.getPageNumber(), queryParam.getPageSize() , queryDTO) ;
        Page<ClazzDTO>  page  = clazzApi.queryPageList(queryModel) ;
        return ResponseEntity.ok().body(page) ;
    }

    /**
     * 获取校区列表
     * @param queryVO 请求参数
     *
     * @return 响应正文
     */
    @RequestMapping("/list")
    public ResponseEntity<?> list(ClazzQueryVO queryVO) {
        ClazzQueryDTO clazzQueryDTO =  CustomBeanUtil.copyProperties(queryVO, new ClazzQueryDTO()) ;
        List<?> list = clazzApi.findClassByParam(clazzQueryDTO, 24, true);
        List<?> data = null;
        if (CollectionUtil.isNotEmpty(list)) {
            data = list.stream().map(item -> CustomBeanUtil.copyProperties(item, new ClazzVO())).collect(Collectors.toList());
        }
        return ResponseEntity.ok().body(data);
    }

    @GetMapping("/{clazzId}/live/broadcast")
    public ResponseEntity<Boolean> isLive(@PathVariable("clazzId") Integer clazzId) {
        return ResponseEntity.ok(clazzApi.isLive(clazzId));
    }

}
